import org.jsecurity.crypto.hash.Md5Hash
import rowingmanager.domain.Country

class BootStrap {

      def init = { servletContext ->
      Random random = new Random()
      def firstNames = ["Greg","Fred","Doug","Craig","Dustin","Steve","Kristy","Rick","Wilma","Simon","Danijel","Lojze","Mark","Lizzy"]
      def lastNames = ["Smith","Flinstone","Abbot","Williams","Adams","Goober","Brady","Jones","Heffernen","Polak","Lewinski","Gobber","Lukather","Broms"]
      def countryNames = ["Slovenia","Slovaska","England","Russia","Macedonia","Usa","Italy","Bolgaria","Germany"]
      def cityNames = ["Ljubljana","New York","London","Skopje","Bratislava","Piran","Moskva","Paris","Bern"]
      def orgTitles = ["Rowing club London","Rowing center Pakistan","Rowing association of England","Boathouse NY","RC Isola"]
      def idTypes = ["passport","emso","ssn"]
      def pos = ["rower","rower","rower","coach","president"]
      def i = 0
      def success = 0

       if(Country.count()== 0){
       1.upto(5){
           i++
          def name = firstNames[random.nextInt(14)]
          def last = lastNames [random.nextInt(14)]
          def c = countryNames [random.nextInt(9)]
          def city = cityNames [random.nextInt(9)]
          def title = orgTitles[random.nextInt(5)]
          def position = pos[random.nextInt(5)]
          
          def country = new Country (name:c,code:"SL_hardcoded").save()

         


        /*  def address2 = new rowingmanager.domain.Address (street:"Cesta na klanec ",streetNumber:"77",city:city,postalCode:random.nextInt(5000)+1000,state:"NONE/US",country:country)


         if(!address2) {
           println "address is null!"
         } else{
          if(!address2.save()){
            address2.errors.each {println it}
          }
         }

          def rower1 = new rowingmanager.domain.Rower(email:"row@klub.com",password:new Md5Hash("rower2").toHex(),address:address2,weight:60,height:190)
          def rower2 = new rowingmanager.domain.Rower(email:"rower2@klub.com",password:new Md5Hash("rower2").toHex(),address:address2,weight:60,height:190)
           def organization = new rowingmanager.domain.Organization(title:title,phone:"00324567",taxid:"888988883834",type:"club",address:address2)
           organization.addToRowers(rower1)
           organization.addToRowers(rower2)
         if(!organization){
           println "it's  null"
         }
          def user = new rowingmanager.domain.User(email:"klub@klub.com",password:new Md5Hash("admin").toHex(),address:address2)
          organization.addToUsers(user)
         if(organization.save())
              println "Success: Saved organization!"
         else {
              println "Failure, couldn't save an organization"
              organization.errors.each{
               println it
             }
          }

        /* def id_list = []
          1.upto(random.nextInt(5)+1){
              def id = idTypes[random.nextInt(3)]
              id_list.add(new rowingmanager.domain.Identification(type:id,identification_number:random.nextInt(8888)))
          }
          def address_list = []
          address_list.add(address)
          address_list.add(address2)
          address_list.add(address3)     */
       /*   def user = new rowingmanager.domain.Rower(name:name,surname:last,password:"bla",position:position,birthday:new Date()
              ,phone:"090234345",email:"ba@fd.com",sex:true,identifications:id_list,addresses:address_list
              ,weight:80,height:190).save()
          organization.addToUsers(user)

            if(user.hasErrors()){
              user.errors.each{
                  println it
              }
          }
         if (user.save()) success++       */

          

       }
       }
    //   def regatta = new rowingmanager.domain.Regatta(organization:rowingmanager.domain.Organization.get(1),title:"Henley Cup",startDay:new Date(),address:rowingmanager.domain.Address.get(1))
    //   def registration = new rowingmanager.domain.Registration()
    //   regatta.save()

        //tole dela..naslednjič dodaj še registrante v regatto.

        //System.setProperty("jmagick.systemclassloader", "no"); //jmagic
     }
     def destroy = {
     }
} 